//
//  ListenViewController.swift
//  xmly
//
//  Created by 刘文超 on 2019/12/27.
//  Copyright © 2019 刘文超. All rights reserved.
//

import UIKit
import FSPagerView
class ListenViewController: UIViewController {

   
    lazy var bannerView:FSPagerView = {
        let bannerView = FSPagerView.init()
        bannerView.alwaysBounceHorizontal = true
        bannerView.automaticSlidingInterval = 3
        bannerView.delegate = self
        bannerView.dataSource = self
        bannerView.isInfinite = true//是否具有无限数量的项目
        bannerView.itemSize = CGSize.init(width: ScreenWidth, height: 180)
        bannerView.interitemSpacing = 0
        bannerView.backgroundColor = UIColor.white
        bannerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "bannercell")
        return bannerView
    }()
    
    
    lazy var collectionView:UICollectionView = {
        let flowlayout  = UICollectionViewFlowLayout.init();
        flowlayout.minimumLineSpacing = KSmallspace
        flowlayout.minimumInteritemSpacing = KSmallspace
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: flowlayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = UIColor.white
        collectionView.contentInset = UIEdgeInsets.init(top: ScreenWidth*0.467, left: 0, bottom: 0, right: 0)
        collectionView.register(ListenItemsCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.register(ListenHeadsCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headCell")
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footCell")
        collectionView.register(ListenItemZhuanTiCollectionViewCell.self, forCellWithReuseIdentifier: "cell1")
       return collectionView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
        
    }
    
    func setUI() {
        
        self.view.addSubview(collectionView)
               collectionView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
               }
        
    
        self.view.addSubview(bannerView)
        bannerView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(SafeAreaTopHeight)
            make.height.equalTo(collectionView.contentInset.top)
        }
    }

}
extension ListenViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 2 {
            return 1
        }
        return Int(arc4random()%7)+1
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 2 {
            let cell:ListenItemZhuanTiCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! ListenItemZhuanTiCollectionViewCell
            cell.backgroundColor = UIColor.red
                    return cell
        }else{
            let cell:ListenItemsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ListenItemsCollectionViewCell
                   cell.backgroundColor = arc4randomColor
                return cell
        }
       
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 2 {
            return CGSize.init(width: ScreenWidth, height: 40)
        }else{
          return CGSize.init(width: (ScreenWidth-KBlackspace*2-KBlackspace*2)/3, height: (ScreenWidth-KBlackspace*2-KBlackspace*2)/3*1.2)
        }
       
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 2 {
            return UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        }
        return UIEdgeInsets.init(top: KBlackspace, left: KBlackspace, bottom: KBlackspace, right: KBlackspace)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 2 {
            return CGSize.init(width: ScreenWidth, height: 0.01)
        }else{
            return CGSize.init(width: ScreenWidth, height: 40)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: ScreenWidth, height: 10)
    }
    
   
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
         if kind == UICollectionView.elementKindSectionFooter {
                   let footCell:UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footCell", for: indexPath)
                    footCell.backgroundColor = lineGrayColor
            
                return footCell
                   
               }else{
            let headCell:ListenHeadsCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headCell", for: indexPath) as! ListenHeadsCollectionReusableView
            if indexPath.section == 2 {
                headCell.isHidden = true
            }else{
                 headCell.isHidden = false
            }
                   return headCell
                   
               }
    }

}

extension ListenViewController:FSPagerViewDelegate,FSPagerViewDataSource{
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return 5
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell:FSPagerViewCell = pagerView.dequeueReusableCell(withReuseIdentifier: "bannercell", at: index)
        cell.imageView?.kf.setImage(with: URL.init(string: "http://pic257.nipic.com/file/20191203/24977360_145740057000_2.jpg"))
        return cell
        
    }
    
    
    
    
}
